//
//  TFQuestionTableViewController.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/4/25.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFQuestionTableViewController: UITableViewController {

    let nodataView = UIView()
    //滚动到回复的cell
    var replyrow:NSInteger = 0
    let dataSource = NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.tableView.separatorStyle = .none
        self.tableView.register(TFQuestionCell.self, forCellReuseIdentifier: "TFQuestionCell")
    }
    
   @objc public func createUI()
    {

        self.nodataView.frame = self.view.bounds
        self.view.addSubview(self.nodataView)
        
        let noImg = UIImageView()
        noImg.frame = CGRect(x: (self.nodataView.frame.size.width-200)/2.0, y: 60, width: 200, height: 150)
        noImg.image = UIImage.init(named: "HTLive.bundle/Default page_ask")
        self.nodataView.addSubview(noImg)
        
        let noLabel = UILabel()
        noLabel.text = "暂无提问"
        noLabel.font = UIFont.PingFang(size: 16)
        noLabel.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1)
        noLabel.textAlignment = .center
        noLabel.frame = CGRect(x: 0, y: 176, width: self.nodataView.frame.size.width, height: 22)
        self.nodataView.addSubview(noLabel)
        
    }

    /**回复提问*/
    
    @objc public func pePly_Question(model:TFQuestionData)
    {
        let copyArray = NSMutableArray.init(array: self.dataSource)
        
        var answer:Bool = false
        for i in 0..<copyArray.count {
            let obj = copyArray[i] as! TFQuestionData
            //本地自己的提问中是否查询到
             if (obj.qid == model.qid) {
                 answer = true
                 self.dataSource.replaceObject(at: i, with: model)
               
                 if (model.isMe) {
                     self.replyrow = i;
                   
                 }
            
                break;
            }
            
        }
        
        //新增新的
        if (answer==false) {
            self.dataSource.add(model)
     
            
        }
        self.tableView.reloadData()
        
        if (self.dataSource.count > 0) {
            self.nodataView.isHidden = true
        } else {
            self.nodataView.isHidden = false
        }
    }

    /**接收提问*/
    
    @objc public func update_Question(model:TFQuestionData)

    {
        for i in stride(from: 0, to: self.dataSource.count ,by: 1) {

            let obj = self.dataSource[i] as! TFQuestionData
            if (obj.qid == model.qid) {
              return;
          }
        }
        self.dataSource.add(model)

        self.tableView.reloadData()

        
        let indexpath = NSIndexPath.init(row: self.dataSource.count - 1, section: 0)
        
        self.tableView.scrollToRow(at: indexpath as IndexPath, at: .bottom, animated: true)
     
        
        if (self.dataSource.count > 0) {
            self.nodataView.isHidden = true
        } else {
            self.nodataView.isHidden = false
        }
    }

    /**删除*/
    
    @objc public func remove_Question(model:TFQuestionData)

    {
        let copyArray = NSMutableArray.init(array: self.dataSource)
        for i in 0..<copyArray.count {
            let obj = self.dataSource[i] as! NSObject
            if (obj is TFQuestionData) {
                let data = obj as! TFQuestionData
                if (data.qid == model.qid) {
                    if (model.deleteQuestionID.count > 0) {
                        
                        for i in 0..<data.answer.count {
                            let answer = data.answer[i] as! TFQuestionAnswer

                            if (answer.qid == model.deleteQuestionID) {

                                data.answer.remove(answer)
                                break;
                            }
                        }
                    } else {
                        self.dataSource.remove(data)

                        break;
                    }
                }
            }
        }
        self.tableView.reloadData()
        if (self.dataSource.count > 0) {
            self.nodataView.isHidden = true
        } else {
            self.nodataView.isHidden = false
        }
    }
 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count;
    }

   // #pragma mark - Table view data source

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model = self.dataSource[indexPath.row] as! TFQuestionData
        
        let height = self.getHeight(model: model)
        
        return height

    }
    
    func getHeight(model:TFQuestionData)->CGFloat
    {
        let nickname = model.nickname + " "
        let str11 = nickname + model.content
        
        let  AttributedString = NSMutableAttributedString.init(string: str11)
        
        let size = TalkfunSharedTools.getSizeLabel(labelString: AttributedString, labelWidth:self.tableView.frame.width-35-16-12-16, labelfont: UIFont.PingFang(size: 14))
        
        
        if (model.answer.count > 0) {
            var height:CGFloat = 0
            var anserwidth:CGFloat = 0
            for i in 0..<model.answer.count{
         
                 let answer = model.answer[i] as! TFQuestionAnswer
                    
                let str = answer.nickname + answer.content
                
                let AttributedString = NSMutableAttributedString.init(string: str)
                
                let font14 = UIFont.PingFang(size: 14)
                let attr14: [NSAttributedString.Key : Any] = [.font: font14 as Any,]
                
                AttributedString.addAttributes(attr14, range: NSRange(location: 0, length: AttributedString.length))
                
              

                let attch = NSTextAttachment()
                attch.bounds = CGRect(x: 0, y: -3, width: 27, height: 15)
                let imageStr = NSAttributedString.init(attachment: attch)

                AttributedString.insert(imageStr, at: answer.nickname.count+1)
                AttributedString.insert(NSAttributedString.init(string: " "), at: answer.nickname.count+2)
                AttributedString.addAttribute(NSAttributedString.Key.kern, value: (5), range: NSRange(location: answer.nickname.count+2, length: 1))
         

                let answersize = TalkfunSharedTools.getSizeLabel(labelString: AttributedString, labelWidth:self.tableView.frame.width-35-16-12-16, labelfont: UIFont.PingFang(size: 14))
               
                
                height = height+answersize.height+10;
               //取最大值
     
                if answersize.width>anserwidth {
                    anserwidth = answersize.width
                }
                
            }

            return size.height+10+1+height+8;

        }
        else {
            
            return size.height+8+12;
           
        }

    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let  cell = tableView.dequeueReusableCell(withIdentifier: "TFQuestionCell") as! TFQuestionCell
       
    
               cell.cellwidth = self.view.frame.size.width;
       
        cell.selectionStyle = .none
               cell.isQuestion = true
               cell.isOrientationLandscape = false
               cell.isChatArea = false;
               let  model = self.dataSource[indexPath.row] as! TFQuestionData
               cell.model = model
               return cell;
    }

    @objc public func scorellToreplyCell()
     {
         if self.dataSource.count>0 {
             let indexpath = NSIndexPath.init(row: self.dataSource.count - 1, section: 0)
             self.tableView.scrollToRow(at: indexpath as IndexPath, at: .bottom, animated: true)
         }
         
         
     }
}
